/*
 * This servlet prepares the information for the users profile
 */
package controllers.classes;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Course;
import models.Employee;
import models.Role;
import models.Student;
import models.User;
import dao.CourseDao;
import dao.EmployeeDao;
import classes.HibernateUtil;
import dao.NoteDao;
import dao.RoleDao;
import dao.StudentDao;
import dao.UserDao;
import models.Note;
import org.hibernate.Session;

/**
 *
 * @author Michel & Ahmed
 */
public class userProfileController extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HibernateUtil hu = new HibernateUtil();
        Session sessie = hu.openSession();
        UserDao ud = new UserDao(sessie);
        StudentDao sd = new StudentDao(sessie);
        EmployeeDao ed = new EmployeeDao(sessie);
        CourseDao cd = new CourseDao(sessie);
        long memberid = Long.parseLong(request.getParameter("id"));

        //Set objects
        User user = ud.get(memberid);
        Student student = new Student();
        Employee member = new Employee();
        Course course = new Course();

        request.setAttribute("userD", user);

        if (ed.get(user.getMember_id()) != null) { //if the profile is a teacher
            member = ed.get(user.getMember_id());
            List<Course> courselist = cd.findAllByTeacher(user.getMember_id()); //get all  courses given by the teacher

            //Prepare request attributesF
            request.setAttribute("isStudent", false);
            request.setAttribute("courses", courselist); //courses given
            request.setAttribute("userFullDetails", member);

        } else { //it is a student
            List<Course> courselist = cd.findAllByStudent(memberid); //get the courses the student is enrolled in
            request.setAttribute("isStudent", true);
            student = sd.get(user.getMember_id());

            //Prepare request attributes
            request.setAttribute("courses", courselist);//courses attending 
            request.setAttribute("userFullDetails", student);
        }

        //-------------------------------------------------------------
        //Prepare the notes anyway...
        //-------------------------------------------------------------
        NoteDao nd = new NoteDao(hu.openSession());
        List<Note> notes = nd.findNoteByUser(memberid);
        request.setAttribute("note", notes);

        hu.close(); //close connection

        RequestDispatcher dispatcher = dispatcher = request.getRequestDispatcher("jsp/member_profile.jsp");
        dispatcher.forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
